package com.tinxing.bp.base.controller;

/**
 * @author YJH
 * @date 2019/12/6 22:48
 */

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tinxing.bp.commons.utils.enums.BizExceptionEnum;
import com.tinxing.bp.commons.utils.kit.HttpKit;
import com.tinxing.bp.commons.utils.tips.ResponseTip;
import com.tinxing.bp.commons.utils.tips.SuccessTip;
import com.tinxing.bp.commons.utils.tips.Tip;
import com.tinxing.bp.commons.utils.util.page.PageInfo;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 基础控制器，用于处理返回Tip和request/response/session
 * @author ZJL
 * @date 2019年9月18日 下午8:36:35
 */
public class BaseController {

    /**
     * success 是否成功
     * @param success
     * @return
     */
    protected Tip tip(boolean success){
        return new Tip(success);
    }

    /**
     * success 是否成功
     * @param success success成功
     * @param message 返回消息
     * @return
     */
    protected Tip errorTip(boolean success, String message){
        return new Tip(success, message);
    }

    /**
     * 返回成功（不推荐使用，swagger不明结果集）
     * @param args
     * @return
     */
    protected JSONObject successTip(Object... args) {
        SuccessTip successTip = new SuccessTip();
        return successTip.getSuccessTip(args);
    }

    protected <T> ResponseTip<T> successTip(T t){
        ResponseTip<T> tip = new ResponseTip<T>(true);
        tip.setData(t);
        return tip;
    }

    protected Tip errorTip(){
        return new Tip(BizExceptionEnum.OPERATION_FAILED);
    }

    protected Tip errorTip(BizExceptionEnum bizException){
        return new Tip(bizException);
    }

//    protected String getUserId(){
//        return JWTUtil.getUserId(HttpKit.getRequest());
//    }

    /**
     * 获取 HttpServletRequest
     */
    protected HttpServletRequest getHttpServletRequest() {
        return HttpKit.getRequest();
    }

    /**
     * 获取 HttpServletResponse
     */
    protected HttpServletResponse getHttpServletResponse() {
        return HttpKit.getResponse();
    }

    /**
     * 将请求的所有参数设置到一个Map中返回
     */
    public Map<String,Object> dealRequest(){
        return HttpKit.dealRequest();
    }

    /**
     * 将请求的所有参数设置到一个Map中返回
     */
    public Map<String,Object> dealRequest(HttpServletRequest request){
        return HttpKit.dealRequest(request);
    }

    /**
     * 获取session
     */
    protected HttpSession getSession() {
        return HttpKit.getSession();
    }

    /**
     * 获取指定的sessionKey
     */
    protected static <T> T getSessionAttr(String key) {
        return HttpKit.getSessionAttr(key);
    }

    /**
     * 设置指定的sessionKey的值
     */
    protected static void setSessionAttr(String key, Object value) {
        HttpKit.setSessionAttr(key, value);
    }

    /**
     * 移除指定的sessionKey
     */
    protected static void removeSessionAttr(String key) {
        HttpKit.removeSessionAttr(key);
    }

    /**
     * 销毁session
     */
    protected static void invalidateSession() {
        HttpKit.invalidateSession();
    }

    protected <T> PageInfo<T> packForBT(IPage<T> page) {
        return new PageInfo<T>(page);
    }
    /**
     * 把service层的分页信息，封装为bootstrap table通用的分页封装
     */
    protected <T> PageInfo<T> packForBT(Page<T> page) {
        return new PageInfo<T>(page);
    }

    /**
     * 删除cookie
     */
    protected void deleteCookieByName(String cookieName) {
        Cookie[] cookies = this.getHttpServletRequest().getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(cookieName)) {
                Cookie temp = new Cookie(cookie.getName(), "");
                temp.setMaxAge(0);
                this.getHttpServletResponse().addCookie(temp);
            }
        }
    }

    /**
     * 删除所有cookie
     */
    protected void deleteAllCookie() {
        Cookie[] cookies = this.getHttpServletRequest().getCookies();
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                Cookie temp = new Cookie(cookie.getName(), "");
                temp.setMaxAge(0);
                this.getHttpServletResponse().addCookie(temp);
            }
        }
    }


}

